package com.jhxy.algorithm.twosum;

import java.util.Random;

/**
 * 暴力求解算法:两数之和
 */
public class TwoSumRandom {


    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];

        Random r = new Random();
        int len = nums.length;

        for (int count = 0; count < 10 * len * len; count++) {
            int i= r.nextInt(len);
            int j;
            do {
                j = r.nextInt(len);
            } while (i == j);

            if (nums[i] + nums[j] == target) {
                return new int[]{i,j};
            }
        }

        return res;
    }

    public static void main(String[] args) {

        InputForTwoNumberSum inputs = new InputForTwoNumberSum("2,7,11,15", "26");
        TwoSumRandom twoSumRandom = new TwoSumRandom();
        for (int i : twoSumRandom.twoSum(inputs.nums, inputs.target)) {
            System.out.println(i);
        }
    }
}
